﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace DbHelper.Config
{
    public class Section : ConfigurationSection
    {
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public DriverCollection Drivers
        {
            get { return (DriverCollection)this[""]; }
        }
    }

    public class DriverCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new DriverElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DriverElement)element).Name;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        protected override string ElementName
        {
            get
            {
                return "driver";
            }
        }

        public new DriverElement this[string name]
        {
            get
            {
                return (DriverElement)base.BaseGet(name);
            }
        }
    }

    public class DriverElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return this["name"].ToString(); }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("assembly", IsRequired = true)]
        public string Assembly
        {
            get { return this["assembly"].ToString(); }
            set { this["assembly"] = value; }
        }

        [ConfigurationProperty("dbConnection", IsRequired = true)]
        public DBConnectionElement DBConnection
        {
            get { return (DBConnectionElement)this["dbConnection"]; }
            set { this["dbConnection"] = value; }
        }

        [ConfigurationProperty("dbDataAdapter", IsRequired = true)]
        public DBDataAdapterElement DBDataAdapter
        {
            get { return (DBDataAdapterElement)this["dbDataAdapter"]; }
            set { this["dbDataAdapter"] = value; }
        }

        [ConfigurationProperty("dbCommandBuilder")]
        public DBCommandBuilderElement DBCommandBuilder
        {
            get { return (DBCommandBuilderElement)this["dbCommandBuilder"]; }
            set { this["dbCommandBuilder"] = value; }
        }

        [ConfigurationProperty("connectionString")]
        public ConnectionStringElement ConnectionString
        {
            get { return (ConnectionStringElement)this["connectionString"]; }
            set { this["connectionString"] = value; }
        }
    }

    public class DBConnectionElement : ConfigurationElement
    {
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return this["type"].ToString(); }
            set { this["type"] = value; }
        }
    }

    public class DBDataAdapterElement : ConfigurationElement
    {
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return this["type"].ToString(); }
            set { this["type"] = value; }
        }
    }

    public class DBCommandBuilderElement : ConfigurationElement
    {
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return this["type"].ToString(); }
            set { this["type"] = value; }
        }
    }

    public class ConnectionStringElement : ConfigurationElement
    {
        [ConfigurationProperty("value", IsRequired = true)]
        public string Value
        {
            get { return this["value"].ToString(); }
            set { this["value"] = value; }
        }
    }
}
